<?php

namespace App\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\Command\Annotation\Command;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;
use App\Service\CommodityService\SyncBdGoodsMixInfoService;
use App\Service\Code\Normal;

/**
 * @Command
 */
class SyncBdGoodsMixInfo extends HyperfCommand {
    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var string
     */
    protected $updateTimeStart;

    /**
     * @var string
     */
    protected $updateTimeEnd;

    public function __construct(ContainerInterface $container) {
        $this->container = $container;

        parent::__construct('syncBdGoodsMixInfo');
    }

    public function configure() {
        parent::configure();
        $this->setDescription('增量同步百大商品供应商商品及商品主资料相关属性');
    }

    /**
     * 开始执行命令
     * 指定了时间范围 php bin/hyperf.php syncBdGoodsMixInfo '2024-06-18 00:00:00'  '2024-06-29 00:00:00'
     * 默认时间 php bin/hyperf.php syncBdGoodsMixInfo
     */
    public function handle() 
    {
        $param               = $this->input->getArguments();
        $updateTimeEndTime   = time();
        $updateTimeStartTime = $updateTimeEndTime - 60 * 60;
        $updateTimeStart     = date("Y-m-d H:i:s",$updateTimeStartTime);
        $updateTimeEnd       = date("Y-m-d H:i:s",$updateTimeEndTime);
        try {
            //开始处理
            $res = SyncBdGoodsMixInfoService::instance()->sync(['updateTimeStart' => $updateTimeStart, 'updateTimeEnd' => $updateTimeEnd]);
            if ($res['code'] != Normal::SUC) {
                $this->error("增量同步百大商品供应商商品及商品主资料相关属性【失败】时间{$updateTimeStart}至{$updateTimeEnd}" . json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            } else {
                $this->info("增量同步百大商品供应商商品及商品主资料相关属性【成功】时间{$updateTimeStart}至{$updateTimeEnd}" . json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            }
        } catch ( \Throwable $th ) {
            print_r([
                'errmsg' => $th->getMessage(),
                'errfile' => $th->getFile(),
                'errline' => $th->getLine(),
            ]);
        }
        echo "增量更新数据已完成\r\n";
    }

    /**
     * 开始执行命令
     * 指定了时间范围 php bin/hyperf.php syncBdGoodsMixInfo '2024-06-18 00:00:00'  '2024-06-29 00:00:00'
     * 默认时间 php bin/hyperf.php syncBdGoodsMixInfo
     */
    public function handleOld() 
    {
        $param      = $this->input->getArguments();
        $start_date = isset($param['updateTimeStart']) && !empty($param['updateTimeStart']) ? strtotime($param['updateTimeStart']) : strtotime('2024-06-15');
        $end_date   = isset($param['updateTimeEnd']) && !empty($param['updateTimeEnd']) ? strtotime($param['updateTimeEnd']) : strtotime('2024-07-04');
        $diff       = ($end_date - $start_date) / 86400;
        if ($diff < 1) {
            $this->error("时间范围跨度要大于等于1天");
            return false;
        }
        for ($i = 1; $i <= $diff; $i++) 
        {
            $end_date_new    = $start_date + ($i * 86400);
            $start_date_new  = $end_date_new - 86400;
            
            for ( $h = 1 ; $h <= 24 ; $h ++ ) 
            {
                $new_end_h_time     = $start_date_new + $h * 3600;
                $new_start_h_time   = $new_end_h_time - 3600;
                
                $updateTimeStart = date('Y-m-d H:i:s', $new_start_h_time);
                $updateTimeEnd   = date('Y-m-d H:i:s', $new_end_h_time);
                
                echo "开始处理日期:{$updateTimeStart}--{$updateTimeEnd}\r\n";
                
                try {
                    //开始处理
                    $res = SyncBdGoodsMixInfoService::instance()->sync(['updateTimeStart' => $updateTimeStart, 'updateTimeEnd' => $updateTimeEnd]);
                    if ($res['code'] != Normal::SUC) {
                        $this->error("增量同步百大商品供应商商品及商品主资料相关属性【失败】时间{$updateTimeStart}至{$updateTimeEnd}" . json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
                    } else {
                        $this->info("增量同步百大商品供应商商品及商品主资料相关属性【成功】时间{$updateTimeStart}至{$updateTimeEnd}" . json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
                    }
                } catch ( \Throwable $th ) {
                    print_r([
                       'errmsg' => $th->getMessage(), 
                       'errfile' => $th->getFile(), 
                       'errline' => $th->getLine(), 
                    ]);
                }
            }
            
           /*  $updateTimeStart = date('Y-m-d H:i:s', $start_date_new);
            $updateTimeEnd   = date('Y-m-d H:i:s', $end_date_new);
            
            echo "开始处理日期:{$start_date_new}\r\n";
            //开始处理
            $res = SyncBdGoodsMixInfoService::instance()->sync(['updateTimeStart' => $updateTimeStart, 'updateTimeEnd' => $updateTimeEnd]);
            if ($res['code'] != Normal::SUC) {
                $this->error("增量同步百大商品供应商商品及商品主资料相关属性【失败】时间{$updateTimeStart}至{$updateTimeEnd}" . json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            } else {
                $this->info("增量同步百大商品供应商商品及商品主资料相关属性【成功】时间{$updateTimeStart}至{$updateTimeEnd}" . json_encode($res, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
            } */
        }
        
        
        echo "增量更新数据已完成\r\n";
        sleep(86400);
    }

    protected function getArguments() {
        return [
            ['updateTimeStart', InputArgument::OPTIONAL, '修改时间开始Y-m-d H:i:s'],
            ['updateTimeEnd', InputArgument::OPTIONAL, '修改时间结束Y-m-d H:i:s']
        ];
    }
}